/*
	节点信息修改
*/

//登录信息
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

$(function(){
	layui.use(['layer', 'form'], function(){
		var layer = layui.layer, form = layui.form;

		var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken};

		function getFreightNodeData(abbr){
			/*
				获取物流节点相关信息
				接口使用GET 方法
				注意传入abbr 参数
			*/
			var _abbr = abbr || getParam('abbr')
			var _ajax = $.extend(ajaxData, {abbr: _abbr})
			
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });

			$.ajax({
				url: BASE_SERVICEURL + URlS.freight,
				type: 'GET',
				data: _ajax,
				success: function(responseData){
					var code = responseData.msg.code;
					if(code == 200){
						//console.log(responseData)
						initFormdata(responseData)
						layer.close(loadIndex)
					}else if(code == 2005){
						//登录失效
						layer.msg('登录失效，请重新登陆后重试', {time: 2000, end: function(){
							parent.layer.close(layer.index);
                        	window.parent.location.reload();
						}});
					}else if(code == 2002){
						//缺少参数
						layer.msg('接口缺少必要参数，请确认数据均以填写后重试！', {time: 2000});
						layer.close(loadIndex)
					}
				},
				error: function(responseData){
					layer.msg(MESSAGES.service_error , { time: 2000, end: function(){
                    	window.location.href = './500.html';
                    }});
				}
			}) 
		};

		function modifyFreightNode(callData){
			/*
				修改物流节点信息
				接口使用post方法
			*/
			var _ajax = $.extend(ajaxData, callData)
			
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });

			$.ajax({
				url: BASE_SERVICEURL + URlS.freight,
				type: 'PUT',
				data: _ajax,
				success: function(responseData){
					var code = responseData.msg.code;
					if(code == 200){
						//console.log(responseData)
						layer.close(loadIndex)
						layer.msg('修改物流节点信息成功！', {time: 2000, end: function(){
                        	window.location.reload();
						}});
					}else if(code == 2005){
						//登录失效
						layer.msg('登录失效，请重新登陆后重试', {time: 2000, end: function(){
							parent.layer.close(layer.index);
                        	window.parent.location.reload();
						}});
					}else if(code == 2002){
						//缺少参数
						layer.msg('接口缺少必要参数，请确认数据均以填写后重试！', {time: 2000});
						layer.close(loadIndex)
					}
				},
				error: function(responseData){
					layer.msg(MESSAGES.service_error , { time: 2000, end: function(){
                    	window.location.href = './500.html';
                    }});
				}
			});
		}

		function deleteFreightNode(abbr){
			/*
				删除物流节点信息
				接口使用 delete 方法
				数据不可恢复 谨慎使用
			*/
			var _abbr = abbr || getParam('abbr')
			var _ajax = $.extend(ajaxData, {abbr: _abbr})
			
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });

			$.ajax({
				url: BASE_SERVICEURL + URlS.freight,
				type: 'DELETE',
				data: _ajax,
				success: function(responseData){
					var code = responseData.msg.code;
					if(code == 200){
						//console.log(responseData)
						layer.close(loadIndex)
						layer.msg('删除物流节点成功，正在为您跳转...', {time: 2000, end: function(){
							parent.layer.close(layer.index);
                        	window.parent.location.reload();
						}});
					}else if(code == 2005){
						//登录失效
						layer.msg('登录失效，请重新登陆后重试', {time: 2000, end: function(){
							parent.layer.close(layer.index);
                        	window.parent.location.reload();
						}});
					}else if(code == 2002){
						//缺少参数
						layer.msg('接口缺少必要参数，请确认数据均以填写后重试！', {time: 2000});
						layer.close(loadIndex)
					}
				},
				error: function(responseData){
					layer.msg(MESSAGES.service_error , { time: 2000, end: function(){
                    	window.location.href = './500.html';
                    }});
				}
			})
		}

		//表单初始化
		function initFormdata(responseData){
			var data = responseData.data[0];
			var init_data = {
				nodeID: data.abbr,
				name: data.name,
				text: data.name_en,
				freightCn: data.single_freight['cn'],
				freightTw: data.single_freight['tw'],
				freightEn: data.single_freight['en'],
				standardCn: data.free_freight_standard['cn'],
				standardTw: data.free_freight_standard['tw'],
				standardEn: data.free_freight_standard['en'],
				status: data.status
			};
			form.val('node-data-form', init_data);
        	form.render();
		}

		//确认修改
		form.on('submit(modify)', function(data){
			layer.confirm('确认以上述信息修改物流节点？', {title:'提示'}, function(index){
				//组织数据
				var f = data.field;
				var callData = {
					'name': f.name,
					'text': f.text,
					'abbr': getParam('abbr') || f.nodeID,
					'freight': langTransObj(f.freightCn, f.freightTw, f.freightEn),
					'free': langTransObj(f.standardCn, f.standardTw, f.standardEn),
					'status': f.status
				};
				//请求修改
				modifyFreightNode(callData)
				layer.close(index)
			})
			return false;
		})
		//确认删除
		form.on('submit(delete)', function(data){
			layer.confirm('确认删除物流节点？数据不可恢复！', {title:'提示'}, function(index){
				deleteFreightNode(getParam('abbr'));
				layer.close(index)
			})
			return false;
		})

		getFreightNodeData(getParam('abbr'))

	})
})




